import { reactive } from 'vue'
class Store {
  constructor() {
    this.appList = reactive([
      {
        id: 0,
        webName: '知乎',
        icon: 'https://files.codelife.cc/website/zhihu.svg',
        webUrl: 'www.zhihu.com',
      },
      {
        id: 1,
        webName: '哔哩哔哩',
        icon: 'https://files.codelife.cc/website/bilibili2.svg',
        webUrl: 'www.bilibili.com',
      },
      {
        id: 2,
        webName: '掘金',
        icon: 'https://files.codelife.cc/website/juejin.svg',
        webUrl: 'www.juejin.com',
      },
      {
        id: 3,
        webName: '新浪微博',
        icon: 'https://files.codelife.cc/website/weibo.svg',
        webUrl: 'www.webo.com',
      },
      {
        id: 4,
        webName: '爱奇艺',
        icon: 'https://files.codelife.cc/website/iqiyi.svg',
        webUrl: 'www.aiqiyi.com',
      },
      {
        id: 5,
        webName: '网易云音乐',
        icon: 'https://files.codelife.cc/website/music163.svg',
        webUrl: 'music.163.com/',
      },
      {
        id: 6,
        webName: '优酷',
        icon: 'https://files.codelife.cc/website/youku.svg',
        webUrl: 'www.youku.com',
      },
    ])
    this.appGroup = reactive([
      {
        title: '分组1',
        appList: [],
      },
    ])
  }

  addApp(value, index) {
    console.log('添加了')
    // 不能简单的push，找到要加载的地方插入
    console.log('插入位置', index)
    this.appList.splice(index, 0, value)
  }
  removeApp(app) {
    this.appList.forEach(function (item, index, array) {
      if (item.webName === app.webName) {
        array.splice(index, 1)
        console.log(array, '执行了')
      }
    })
  }
  move(oldIndex, newIndex) {
    const movedApp = this.appList.splice(oldIndex, 1)
    console.log('移动了', movedApp)
    this.appList.splice(newIndex, 0, JSON.parse(JSON.stringify(movedApp))[0])
    console.log('移动后的applist', this.appList)
  }
  updateApp(newAppInfo) {
    this.appList.forEach((item) => {
      if (item.id === newAppInfo.id) {
        item.webUrl = newAppInfo.webUrl
        item.webName = newAppInfo.webName
        console.log('更新了')
      }
    })
  }
  addGroup() {
    this.appGroup.push({
      title: '新分组',
      appList: [],
    })
  }
  deleteGroup(id) {
    this.appGroup.pop()
  }
  getAppListCount() {
    return this.appList.length
  }
}
const store = new Store()

export default store
